package com.citywithincity.redis.models;

import com.citywithincity.redis.utils.RedisUtil;

import redis.clients.jedis.Jedis;

/**
 * 存放id
 * @author renxueliang
 *
 */
public class RedisListModel {

	private String key;
	
	
	public RedisListModel(String key){
		this.key = key;
	}
	
	
	private String getKey(Object id){
		return new StringBuilder(key).append(':').append(id).toString();
	}
	
	/**
	 * 清空整个
	 */
	public void clear(Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.write();
			jedis.del(getKey(id));
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	/**
	 * 
	 * @param id
	 * @param ids
	 */
	public void push(Object id,String[] ids){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.write();
			jedis.lpush(getKey(id),ids);
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
	public String pop(Object id){
		Jedis jedis = null;
		try{
			jedis = RedisUtil.read();
			return jedis.rpop(getKey(id));
		}finally{
			if(jedis!=null)jedis.close();
		}
	}
	
}
